import { ref } from 'vue'
import request from '@/utils/request'

// 模拟用户数据
const mockUsers = ref([
  {
    username: 'admin',
    password: '123456',
    token: 'mock-admin-token-123456'
  },
  {
    username: 'user',
    password: '123456',
    token: 'mock-user-token-654321'
  }
])

export const login = async (username, password) => {
  // 模拟API延迟
  await new Promise(resolve => setTimeout(resolve, 500))
  
  const user = mockUsers.value.find(u => 
    u.username === username && u.password === password
  )

  if (user) {
    return {
      data: {
        token: user.token,
        username: user.username
      }
    }
  } else {
    throw {
      response: {
        data: {
          message: '用户名或密码错误'
        }
      }
    }
  }
}

export const logout = async () => {
  // 清除token等操作
  return Promise.resolve()
}

export const getUserInfo = async (token) => {
  // 模拟获取用户信息
  await new Promise(resolve => setTimeout(resolve, 300))
  
  const user = mockUsers.value.find(u => u.token === token)
  if (user) {
    return {
      data: {
        username: user.username,
        roles: ['user']
      }
    }
  }
  throw new Error('无效的token')
}
